<?php

namespace app\teacher\controller\student;

use app\common\controller\Frontend;
use app\common\model\banxi\learn\LearnReadingArticle;
use app\common\model\banxi\learn\LearnSkRecord;
use app\common\model\banxi\learn\LearnVocabularyRecord;
use app\common\model\banxi\learn\LearnWordMemorizeRecord;
use app\common\model\banxi\organization\Organization;
use app\common\model\banxi\student\Student as StudentModel;
use think\exception\ValidateException;
use think\facade\Validate;
use think\validate\ValidateRule;

/*
 * 学员
 * */

class Student extends Frontend
{
    protected array $noNeedLogin = [''];

    protected array $noNeedPermission = ['*'];

    public function initialize(): void
    {
        parent::initialize();
    }

    /*
     * 分页-我教授的学员
     * */
    public function listMyTeachStudentPage()
    {
        $limit = $this->request->get('limit', 20);
        $lindex = $this->request->get('index', 1);
        $searchText = $this->request->get('searchText', '');

        $trainerId = $this->auth->id;
        //获取课程学员id
        $LearnSkRecordModel = new LearnSkRecord();
        $studentIds = $LearnSkRecordModel
            ->where(['trainerId' => $trainerId, 'del' => 0, 'isEnd' => 1])
            ->column('studentId');
        $where = [];
        $where[] = ['del', '=', 0];
        if ($studentIds) {
            if ($searchText) {
                $where[] = ['name', 'like', "%{$searchText}%"];
            }
            $studentIds = array_unique($studentIds);
            $where[] = ['id', 'in', $studentIds];
        }
        $StudentModel = new StudentModel();
        $list = $StudentModel->with(['accountClassHour'])
            ->where($where)
            ->paginate([
                'page' => $lindex,
                'list_rows' => $limit,
            ]);

        $this->ok('操作成功', result_row($list));
    }

    /*
     * 查询-学员个人信息
     * */
    public function getStudentInfo()
    {
        $studentId = $this->request->get('studentId', 0);

        $StudentModel = new StudentModel();
        $info = $StudentModel->with([
            'organizationBaseInfo' => function ($query) {
                $query->field('id,name,managerName');
            },
            'accountClassHour',
            'gradeInfo' => function ($query) {
                $query->field('id,name');
            }
        ])->where(['id' => $studentId])
            ->find();
        if (!$info) {
            $this->fail('学员不存在');
        }
        if ($info['organizationId'] == 0) {
            $organization = new Organization();
            $info['organizationBaseInfo'] = $organization->getOrganizationInfo();
        }

        $info['totalWordNum'] = LearnWordMemorizeRecord::countByStudent($studentId, 7, 7);
        $this->ok('获取学员信息', $info);
    }

    /*
     * 分页-学员阅读记录
     * */
    public function listMyReadingRecord()
    {
        $studentId = $this->request->get('studentId', 0);
        $limit = $this->request->get('limit', 20);
        $lindex = $this->request->get('index', 1);
        $searchText = $this->request->get('searchText', '');

        $LearnReadingArticleModel = new LearnReadingArticle();
        $where = [];
        $where[] = ['studentId', '=', $studentId];
        $where[] = ['isSubmit', '=', 1];

        if ($searchText) {
            $where[] = ['articleName', 'like', "%{$searchText}%"];
        }

        $list = $LearnReadingArticleModel
            ->where($where)
            ->order('id', 'desc')
            ->paginate([
                'page' => $lindex,
                'list_rows' => $limit,
            ]);

        $this->ok('操作成功', result_row($list));
    }

    /*
     * 分页-学员测评记录
     * */
    public function listStudentVocabularyPage()
    {
        $studentId = $this->request->get('studentId', 0);
        $limit = $this->request->get('limit', 20);
        $index = $this->request->get('index', 1);

        $LearnVocabularyRecordModel = new LearnVocabularyRecord();
        $where = [];
        $where[] = ['accountId', '=', $studentId];
        $where[] = ['isSubmit', '=', 1];
        $where[] = ['accountType', '=', 3];//测试人类型 1=平台 2=机构 3=学员 4=教练

        $list = $LearnVocabularyRecordModel
            ->with(['settingInfo'])
            ->where($where)
            ->order('id', 'desc')
            ->paginate([
                'page' => $index,
                'list_rows' => $limit,
            ]);

        $this->ok('操作成功', result_row($list));
    }

    /*
     * 查询-学员测评评估报告
     * */
    public function getDetail()
    {
        $id = $this->request->get('id', 0);

        $LearnVocabularyRecordModel = new LearnVocabularyRecord();
        $info = $LearnVocabularyRecordModel->getDetail($id);

        $this->ok('操作成功', $info);
    }

    /**
     * 分页Select查询我的学员
     * @return void
     * @throws \think\db\exception\DbException
     */
    public function listSelectMyStudentClassHourPage(): void
    {
        $param = $this->request->param(["index" => 1, 'limit' => 10, "query" => [], "courseFullType" => 0, 'searchText' => '']);
        try {
            $validate = Validate::rule([
                'courseFullType' => ValidateRule::in([1, 2, 3], '请选择正确的课程类型'),
            ]);
            if (!$validate->check($param)) throw new ValidateException($validate->getError());
        } catch (ValidateException $exception) {
            $this->fail($exception->getMessage());
        }
        if (!empty($param['searchText'])) {
            $param['query']['LIKE_name'] = $param['searchText'];
        }
        $trainer = $this->auth->getUserInfo();

        if ($trainer['organizationId']) {
            $param['query']['EQ_organizationId'] = $trainer['organizationId'];
            //            $param['query']['EQ_organizationId'] = '1933884391692943360';
        }
        $where = get_query($param);

        $pageParams = get_page_params($param);
        //        halt($where);
        $StudentModel = new StudentModel();
        $list = $StudentModel->getListSelectClassHourPage($param['courseFullType'], $where, $pageParams['page'], $pageParams['limit']);
        $this->ok('操作成功', result_row($list));
    }

    /*
     * 我绑定的学生
     */
    public function myTeachStudentList()
    {
        $param = $this->request->param(['index' => 1, 'limit' => 10, 'query' => [], 'searchText']);
        if (!empty($param['searchText'])) {
            $param['query']['LIKE_name'] = $param['searchText'];
        }
        $param['query']['EQ_teachTrainerId'] = $this->auth->id;
        $where = get_query($param);
        $pageParams = get_page_params($param);
        $studentModel = new StudentModel();
        $list = $studentModel->where($where)->field('id,name')->paginate([
            'page' => $pageParams['page'],
            'list_rows' => $pageParams['limit'],
        ]);
        $this->ok('操作成功', result_row($list));
    }

    /*
     * 我绑定的学生课时查询
     */
    public function listSelectClassHourPage()
    {
        $param = $this->request->param(["index" => 1, 'limit' => 10, "query" => [], "courseFullType" => 0, 'searchText' => '']);
        try {
            $validate = Validate::rule([
                'courseFullType' => ValidateRule::in([1, 2, 3], '请选择正确的课程类型'),
            ]);
            if (!$validate->check($param)) throw new ValidateException($validate->getError());
        } catch (ValidateException $exception) {
            $this->fail($exception->getMessage());
        }
        if (!empty($param['searchText'])) {
            $param['query']['LIKE_name'] = $param['searchText'];
        }
        $param['query']['EQ_teachTrainerId'] = $this->auth->id;
        $where = get_query($param);
        $pageParams = get_page_params($param);
        $studentModel = new StudentModel();
        $list = $studentModel->getListSelectClassHourPage($param['courseFullType'], $where, $pageParams['page'], $pageParams['limit']);
        $this->ok('操作成功', result_row($list));

    }
}